package org.jeecg.modules.permissions;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.htxincailiao.ability.entity.HTAbilityDemand;
import org.jeecg.modules.htxincailiao.ability.entity.HTAbilitySupply;
import org.jeecg.modules.htxincailiao.account.constant.HTSysRoleCodeEnum;
import org.jeecg.modules.htxincailiao.account.entity.HTMerchant;
import org.jeecg.modules.htxincailiao.account.entity.HTPlatformUser;
import org.jeecg.modules.htxincailiao.account.service.IHTAccountService;
import org.jeecg.modules.htxincailiao.account.service.IHTMerchantService;
import org.jeecg.modules.htxincailiao.goods.entity.HTGoods;
import org.jeecg.modules.htxincailiao.order.entity.HTOrderGoods;
import org.jeecg.modules.htxincailiao.patent.entity.HTPatentDemand;
import org.jeecg.modules.htxincailiao.patent.entity.HTPatentSupply;
import org.jeecg.modules.htxincailiao.position.entity.HTPosition;
import org.jeecg.modules.htxincailiao.shop.entity.HTShop;
import org.jeecg.modules.htxincailiao.shop.service.IHTShopService;
import org.jeecg.modules.system.entity.SysRole;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysRoleService;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Data
@Component
public class HTRolePermission {
    static String NONE_PERMISSION_FLAG = "-1";

    private static HTRolePermission serverHandler;
    @Resource
    private ISysUserService sysUserService;
    @Resource
    private ISysRoleService sysRoleService;
    @Resource
    private ISysUserRoleService sysUserRoleService;
    @Resource
    private IHTAccountService accountService;
    @Resource
    private IHTShopService shopService;
    @Resource
    private IHTMerchantService merchantService;

    public static HTRolePermissionDTO GetLoginUserPermissionInfo(HttpServletRequest req) {
        HTRolePermissionDTO dto = new HTRolePermissionDTO();
        String adminFlag = req.getHeader(CommonConstant.X_ADMIN_FLAG);

        if (oConvertUtils.isNotEmpty(adminFlag) && adminFlag.equals(CommonConstant.X_ADMIN_FLAG)) {
            dto.setAdminSignedIn(true);
            // 获取登录后端用户
            String token = req.getHeader(CommonConstant.X_ACCESS_TOKEN);
            String username = JwtUtil.getUsername(token);
            if (oConvertUtils.isNotEmpty(username)) {
                List<String> roles = serverHandler.sysUserRoleService.getRoleIdByUserName(username);
                if (oConvertUtils.isNotEmpty(roles) && roles.size() > 0) {
                    String roleId = roles.get(0);
                    dto.setRoleId(roleId);
                    SysRole sysRole = serverHandler.sysRoleService.getById(roleId);
                    String roleCode = sysRole.getRoleCode();
                    if (oConvertUtils.isNotEmpty(roleCode)) {
                        HTSysRoleCodeEnum roleCodeEnum = HTSysRoleCodeEnum.EnumValueOf(roleCode);
                        dto.setRoleCodeEnum(roleCodeEnum);
                    }
                    if (oConvertUtils.isNotEmpty(sysRole.getRoleName())){
                        dto.setRoleName(sysRole.getRoleName());
                    }

                    HTPlatformUser platformUser = serverHandler.accountService.queryFrontendUserByUsername(username);
//                    System.out.printf("\n\n\n platformUser: %s \n\n\n", JSONObject.toJSONString(platformUser));
                    if (oConvertUtils.isNotEmpty(platformUser)) {
                        dto.setPlatformUser(platformUser);
                    }

                    SysUser sysUser = serverHandler.sysUserService.getUserByName(username);
                    if (oConvertUtils.isNotEmpty(sysUser)) {
                        dto.setSysUser(sysUser);
                    }
                }
            }
        }

        return dto;
    }

    public static String GetLoginUserShopId(HttpServletRequest req) {
        HTRolePermissionDTO dto = GetLoginUserPermissionInfo(req);
        HTSysRoleCodeEnum roleCodeEnum = dto.getRoleCodeEnum();
        if (oConvertUtils.isNotEmpty(dto.getPlatformUser())) {
            SysUser platformUser = dto.getSysUser();
            HTMerchant htMerchant = serverHandler.merchantService.getById(platformUser.getShopId());
            if (oConvertUtils.isNotEmpty(htMerchant)) {
                switch (roleCodeEnum) {
                    case ROLE_MERCHANT:
                    case ROLE_EVALUATION_AGENCY:
                    case ROLE_ALLIANCE:
                    case ROLE_ALLIANCE_MEMBER:
                        return htMerchant.getId();
                    default:
                        break;
                }
            }
        }
        return null;
    }

    public static String GetLoginUserId(HttpServletRequest req) {
        HTRolePermissionDTO dto = GetLoginUserPermissionInfo(req);
//        System.out.printf("\n\n\n GetLoginUserId %s \n\n\n", JSONObject.toJSONString(dto));
        if (oConvertUtils.isNotEmpty(dto.getPlatformUser())) {
            HTPlatformUser platformUser = dto.getPlatformUser();
//            HTSysRoleCodeEnum roleCodeEnum = dto.getRoleCodeEnum();
            return platformUser.getId();
        }
        return null;
    }

    public static String GetLoginUserForumsExpertId(HttpServletRequest req) {
        HTRolePermissionDTO dto = GetLoginUserPermissionInfo(req);
        if (oConvertUtils.isNotEmpty(dto.getPlatformUser())) {
            HTPlatformUser platformUser = dto.getPlatformUser();
            HTSysRoleCodeEnum roleCodeEnum = dto.getRoleCodeEnum();
            if (roleCodeEnum.getValue().equals(HTSysRoleCodeEnum.ROLE_FORUMS_EXPERT.getValue())) {
                return platformUser.getId();
            }
        }
        return null;
    }



    public static JSONObject GetShopPermissionInfo(HttpServletRequest req) {
        JSONObject obj = new JSONObject();
        // 获取登录后端用户
        String token = req.getHeader(CommonConstant.X_ACCESS_TOKEN);
        String username = JwtUtil.getUsername(token);
        if (oConvertUtils.isNotEmpty(username)) {
            List<String> roles = serverHandler.sysUserRoleService.getRoleIdByUserName(username);
            if (oConvertUtils.isNotEmpty(roles) && roles.size() > 0) {
                String roleId = roles.get(0);
                SysRole sysRole = serverHandler.sysRoleService.getById(roleId);
                String roleCode = sysRole.getRoleCode();
                obj.put("roleCode", roleCode);

                System.out.printf(" username: %s \n", JSONObject.toJSONString(username));
//                if (roleCode.equals(HTSysRoleCodeEnum.ROLE_SYS_USER.getValue())) {
//                    String shopId = GetAdminShopId();
//                    obj.put("shopId", shopId);
//                }

//                HTPlatformUser platformUser = serverHandler.accountService.queryFrontendUserByUsername(username);

                SysUser sysUser = serverHandler.sysUserService.getUserByName(username);
                System.out.printf(" sysUser %s \n", JSONObject.toJSONString(sysUser));
                if (oConvertUtils.isNotEmpty(sysUser)) {
                    obj.put("shopId", sysUser.getShopId());
                }
            }
        }

        return obj;
    }

    public static void AddGoodsPermission(HttpServletRequest req, HTGoods goods) {
        String shopId = GetLoginUserShopId(req);
        HTRolePermissionDTO dto = GetLoginUserPermissionInfo(req);

        if (oConvertUtils.isNotEmpty(dto.getRoleCodeEnum())) {
            switch (dto.getRoleCodeEnum()) {
                case ROLE_SYS_USER:
                    break;
                case ROLE_FORUMS_EXPERT:
                case ROLE_MERCHANT:
                case ROLE_EVALUATION_AGENCY:
                case ROLE_ALLIANCE: {
                    String targetShopId = oConvertUtils.isNotEmpty(shopId) ? shopId : NONE_PERMISSION_FLAG;
                    goods.setShopId(targetShopId);
                }
                break;
            }
        }
    }

    public static void AddOrderGoodsPermission(HttpServletRequest req, HTOrderGoods orderGoods) {
        String shopId = GetLoginUserShopId(req);
        HTRolePermissionDTO dto = GetLoginUserPermissionInfo(req);

        if (oConvertUtils.isNotEmpty(dto.getRoleCodeEnum())) {
            switch (dto.getRoleCodeEnum()) {
                case ROLE_SYS_USER:
                    break;
                case ROLE_FORUMS_EXPERT:
                case ROLE_MERCHANT:
                case ROLE_EVALUATION_AGENCY:
                case ROLE_ALLIANCE: {
                    String targetShopId = oConvertUtils.isNotEmpty(shopId) ? shopId : NONE_PERMISSION_FLAG;
                    orderGoods.setShopId(targetShopId);
                }
                break;
            }
        }
    }

    public static void AddAbilitySupplyPermission(HttpServletRequest req, HTAbilitySupply abilitySupply) {
        String shopId = GetLoginUserShopId(req);
        HTRolePermissionDTO dto = GetLoginUserPermissionInfo(req);

        if (oConvertUtils.isNotEmpty(dto.getRoleCodeEnum())) {
            switch (dto.getRoleCodeEnum()) {
                case ROLE_SYS_USER:
                    break;
                case ROLE_FORUMS_EXPERT:
                case ROLE_MERCHANT:
                case ROLE_EVALUATION_AGENCY:
                case ROLE_ALLIANCE: {
                    String targetShopId = oConvertUtils.isNotEmpty(shopId) ? shopId : NONE_PERMISSION_FLAG;
                    abilitySupply.setShopId(targetShopId);
                }
                break;
            }
        }
    }

    public static void AddAbilityDemandPermission(HttpServletRequest req, HTAbilityDemand abilityDemand) {
        String shopId = GetLoginUserShopId(req);
        HTRolePermissionDTO dto = GetLoginUserPermissionInfo(req);

        if (oConvertUtils.isNotEmpty(dto.getRoleCodeEnum())) {
            switch (dto.getRoleCodeEnum()) {
                case ROLE_SYS_USER:
                    break;
                case ROLE_FORUMS_EXPERT:
                case ROLE_MERCHANT:
                case ROLE_EVALUATION_AGENCY:
                case ROLE_ALLIANCE: {
                    String targetShopId = oConvertUtils.isNotEmpty(shopId) ? shopId : NONE_PERMISSION_FLAG;
                    abilityDemand.setShopId(targetShopId);
                }
                break;
            }
        }
    }

    public static void AddPositionPermission(HttpServletRequest req, HTPosition position) {
        String shopId = GetLoginUserShopId(req);
        HTRolePermissionDTO dto = GetLoginUserPermissionInfo(req);

        if (oConvertUtils.isNotEmpty(dto.getRoleCodeEnum())) {
            switch (dto.getRoleCodeEnum()) {
                case ROLE_SYS_USER:
                    break;
                case ROLE_FORUMS_EXPERT:
                case ROLE_MERCHANT:
                case ROLE_EVALUATION_AGENCY:
                case ROLE_ALLIANCE: {
                    String targetShopId = oConvertUtils.isNotEmpty(shopId) ? shopId : NONE_PERMISSION_FLAG;
                    position.setEnterpriseId(targetShopId);
                }
                break;
            }
        }
    }

    public static void AddPatentSupplyPermission(HttpServletRequest req, HTPatentSupply patentSupply) {
        String shopId = GetLoginUserShopId(req);
        HTRolePermissionDTO dto = GetLoginUserPermissionInfo(req);

        if (oConvertUtils.isNotEmpty(dto.getRoleCodeEnum())) {
            switch (dto.getRoleCodeEnum()) {
                case ROLE_SYS_USER:
                    break;
                case ROLE_FORUMS_EXPERT:
                case ROLE_MERCHANT:
                case ROLE_EVALUATION_AGENCY:
                case ROLE_ALLIANCE: {
                    String targetShopId = oConvertUtils.isNotEmpty(shopId) ? shopId : NONE_PERMISSION_FLAG;
                    patentSupply.setShopId(targetShopId);
                }
                break;
            }
        }
    }

    public static void AddPatentDemandPermission(HttpServletRequest req, HTPatentDemand patentDemand) {
        String shopId = GetLoginUserShopId(req);
        HTRolePermissionDTO dto = GetLoginUserPermissionInfo(req);

        if (oConvertUtils.isNotEmpty(dto.getRoleCodeEnum())) {
            switch (dto.getRoleCodeEnum()) {
                case ROLE_SYS_USER:
                    break;
                case ROLE_FORUMS_EXPERT:
                case ROLE_MERCHANT:
                case ROLE_EVALUATION_AGENCY:
                case ROLE_ALLIANCE: {
                    String targetShopId = oConvertUtils.isNotEmpty(shopId) ? shopId : NONE_PERMISSION_FLAG;
                    patentDemand.setShopId(targetShopId);
                }
                break;
            }
        }
    }

    //通过@PostConstruct实现初始化bean之前进行的操作
    @PostConstruct
    public void init() {
        serverHandler = this;
        // 初使化时将已静态化的sysUserService实例化
        serverHandler.sysUserService = this.sysUserService;
        serverHandler.sysRoleService = this.sysRoleService;
        serverHandler.sysUserRoleService = this.sysUserRoleService;
        serverHandler.accountService = this.accountService;
        serverHandler.shopService = this.shopService;
    }


}
